*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		2b_tables_analysis_consumption.do
*-------------------------------------------------------------------------------
* Description		Creates consumption cutback  and economic shock tables  
* Outputs		Tables 2, A7, A8, A10
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
*TOC
*1)	Prepare data for analysis
*2)	Tables
*2.1)	Consumption Cutbacks for people who had an economic shock and AES
*2.2)	Consumption Cutbacks for full sample and AES
*2.3)	Probability of experiencing an economic shock tables
*2.4)	Triple Difference Effect on Consumption Cutback of Economic Shock
*===============================================================================

*===============================================================================
* Section 1 - Prepare data for analysis
*===============================================================================

clear all
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Path to processed data 
global results "$path/results"		
cd "$results"									// Change to directory to store results
set more off

*Loading processed data
	use $path/datasets/impactDatabase.dta, clear

*===============================================================================
* Section 2 - Tables 
*===============================================================================
* Section 2.1 - Table on consumption cutback for economic shock sample
*-------------------------------------------------------------------------------
* 2.1.1.Calculates table content ---------------------------------------------
*-------------------------------------------------------------------------------
preserve	
	eststo clear
	local i = 1
	
	foreach y in mealsCutback          meatCutback     medicinesCutback	///
		     schoolSuppliesCutback clothingCutback schoolSnacksCutback	///
	             transportCutback      eatingOutCutback { 

		reg d.`y' 							// Mark subjects nonmissing in both periods
		egen t1 = max(e(sample)), by(idBaseSurvey)                      

		reghdfe `y' c.accountAccess#c.post post if t1                == 1 & 		/// Treatment effect on consumption variables 
		                                           economicShock2009 == 1, 		/// Only for the subsample of people who faced an economic shock
		abs(idBaseSurvey) vce(cluster groupId) 						/// Individual FE, SEs clustered at group level

		//contol mean
		sum `y' if accountAccess == 0 & post == 1 & economicShock2009 == 1 		// Control mean outcome
		if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}

		//Average Effect Size (AES) calculation, step 1: normalizes variables	
		sum `y' if accountAccess == 0 //computes sd
		gen aes_cc`i' = `y' / r(sd)   //normalizes outcome variable


		distinct idBaseSurvey  if e(sample) & economicShock2009 ==1 
		estadd scalar num_ind         = r(ndistinct)
		estadd local  ind_fixeff      = "Yes"
		drop t1
			
		eststo
		local i = `i' + 1 //updates counter	
	}
	
	//AES calculation, step 2: reshapes the data to implement regression
	// we keep only variables required
	keep aes_cc* idBaseSurvey post economicShock2009 groupId accountAccess asesor2008
	reshape long aes_cc, i(idBaseSurvey post asesor2008) j(out)		// "out" is the 8 consumption categories, i.e. the outcome variables used in the regression above 
	
	//AES: Implements regression						// Regressing normalised consumption variables on treatment x post x out to use in AES calculation below 
	reghdfe aes_cc (c.accountAccess#c.post)#i.out c.accountAccess#i.out 	/// 
	c.post#i.out i.out if economicShock2009==1, 				/// 
	abs(idBaseSurvey) vce(cluster groupId) 
	
	//AES: computes AES 							
	lincom( (1.out#c.accountAccess#c.post + 2.out#c.accountAccess#c.post + 	///
		 3.out#c.accountAccess#c.post + 4.out#c.accountAccess#c.post + 	///
		 5.out#c.accountAccess#c.post + 6.out#c.accountAccess#c.post + 	///
		 7.out#c.accountAccess#c.post + 8.out#c.accountAccess#c.post ) / 8)	

	local aes_se = trim("`: display %12.3fc r(se)'")
	local aes_p  = 2*ttail(`r(df)',abs(`r(estimate)' / `r(se)'))
	// stores stars for coefficient
	if (`aes_p' <= 0.1 & `aes_p' > 0.05 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'*")
	}
	else if (`aes_p' <= 0.05 & `aes_p' > 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'**")
	}
	else if (`aes_p' <= 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'***")
	}
	else {
		local aes_b = trim("`: display %12.3fc r(estimate)'")
	}
	
	
*-------------------------------------------------------------------------------
* 2.1.2. Writes tables
*-------------------------------------------------------------------------------
	esttab est* using tab_consumption_cutback_shock_samp.tex, replace 							///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber 						///
	stats( control_mean num_ind N ind_fixeff, fmt(%12.0fc %12.0fc %12.0fc) 							///
	label("Control mean" "\hline Individuals" "Observations" "Individual FE")) mlabels(,none)				///
	collabels(, none) eqlabels(, none) 											/// 
	keep(c.accountAccess#c.post )  												///
	varlabels(c.accountAccess#c.post "Account $\times$ post") 								///
	starlevels(* 0.1 ** 0.05 *** 0.01) 											///
	prehead(\begin{tabular}{p{3.75 cm} l*{@M}{c}} \hline \hline 								///
	& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ 									///
	& Meals & Meat & Medicines & School & Clothing & School & Public & Eating \\ 						///
	& & & & supplies & & snacks & transport & out  \\) 									///
	posthead(\hline) prefoot( ) 												///
	postfoot(\hline \multicolumn{@span}{c}{\textbf{AES:} `aes_b' (`aes_se')} \\ 						///
	\hline \hline \end{tabular}) 	

restore


*-------------------------------------------------------------------------------
* Section 2.2 - Table on consumption cutback (full sample)			
*-------------------------------------------------------------------------------
* 2.2.1.Calculates table content -----------------------------------------------
*-------------------------------------------------------------------------------
	preserve	
	eststo clear
	local i = 1
	
	foreach y in mealsCutback meatCutback medicinesCutback 			///
	schoolSuppliesCutback clothingCutback schoolSnacksCutback 		///
	transportCutback eatingOutCutback { 

		reg d.`y' 							// Mark subjects nonmissing in both periods
		egen t1 = max(e(sample)), by(idBaseSurvey)

		reghdfe `y' c.accountAccess#c.post post if t1 == 1 , 		/// Not restricting to economic shock sample
		abs(idBaseSurvey) vce(cluster groupId) 

		//Mean outcome for the control group 
		sum `y' if accountAccess == 0 & post == 1		 	
		if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}

		//AES calculation, step 1: normalizes variables
		sum `y' if accountAccess==0 //computes sd
		gen aes_cc`i' = `y' / r(sd) //normalizes outcome variable


		distinct idBaseSurvey  if e(sample)
		estadd scalar num_ind = r(ndistinct)
		estadd local ind_fixeff = "Yes"
		estadd local stratum_post_fe = "Yes"
		drop t1
			
		eststo
		local i = `i' + 1 //updates counter	
			
		
	}
	
	//AES calculation, step 2: reshapes the data to implement regression
	// we keep only variables required
	keep aes_cc* idBaseSurvey post economicShock2009 groupId accountAccess asesor2008
	reshape long aes_cc, i(idBaseSurvey post asesor2008) j(out)
	
	//AES: Implements regression
	reghdfe aes_cc (c.accountAccess#c.post)#i.out c.accountAccess#i.out 	///
	c.post#i.out i.out, abs(idBaseSurvey) vce(cluster groupId) 
	
	//AES: computes AES
	lincom( (1.out#c.accountAccess#c.post + 2.out#c.accountAccess#c.post + 	///
		3.out#c.accountAccess#c.post + 4.out#c.accountAccess#c.post + 	///
		5.out#c.accountAccess#c.post + 6.out#c.accountAccess#c.post + 	///
		7.out#c.accountAccess#c.post + 8.out#c.accountAccess#c.post ) / 8)	

	local aes_se = trim("`: display %12.3fc r(se)'")
	local aes_p = 2*ttail(`r(df)',abs(`r(estimate)' / `r(se)'))
	// stores stars for coefficient
	if (`aes_p' <= 0.1 & `aes_p' > 0.05 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'*")
	}
	else if (`aes_p' <= 0.05 & `aes_p' > 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'**")
	}
	else if (`aes_p' <= 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'***")
	}
	else {
		local aes_b = trim("`: display %12.3fc r(estimate)'")
	}
	
	
*-------------------------------------------------------------------------------
* 2.2.2. Writes tables
*-------------------------------------------------------------------------------
	esttab est* using tab_consumption_cutback_full_samp.tex, replace 							///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber 						///
	stats( control_mean num_ind N ind_fixeff, fmt(%12.0fc %12.0fc %12.0fc) 							///
	label("Control mean" "\hline Individuals" "Observations" "Individual FE")) mlabels(,none)				///
	collabels(, none) eqlabels(, none) 											/// 
	keep(c.accountAccess#c.post )  												///
	varlabels(c.accountAccess#c.post "Account $\times$ post") 								///
	starlevels(* 0.1 ** 0.05 *** 0.01) 											///
	prehead(\begin{tabular}{p{3.75 cm} l*{@M}{c}} \hline \hline 								///
	& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ 									///
	& Meals & Meat & Medicines & School & Clothing & School & Public & Eating \\ 						///
	& & & & supplies & & snacks & transport & out  \\) 									///
	posthead(\hline) prefoot( ) 												///
	postfoot(\hline \multicolumn{@span}{c}{\textbf{AES:} `aes_b' (`aes_se')} \\ 						///
	\hline \hline \end{tabular}) 	

restore

*-------------------------------------------------------------------------------
* 2.3 Probability of experiencing a shock --------------------------------------
*-------------------------------------------------------------------------------
*2.3.1 Calculates table content 	
*-------------------------------------------------------------------------------
	
	eststo clear

	reghdfe economicShock c.accountAccess#c.post post if inSample == 1, 	///		
	abs(idBaseSurvey) vce(cluster groupId)                                  

	//Control mean outcome 
	sum economicShock if accountAccess == 0 & post == 1 
	local control_mean = r(mean)
	if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}
		
	estadd local ind_fixeff = "Yes"	
	distinct idBaseSurvey  if e(sample)
	estadd scalar num_ind = r(ndistinct)
        
	eststo

*-------------------------------------------------------------------------------
* 2.3.2 Writes table 
*-------------------------------------------------------------------------------
	
	esttab est* using tab_probshock.tex, replace 										///
	cells(b(star fmt(3)) se(par fmt(3))) label style(tex) nonumber								///
	stats( control_mean num_ind N ind_fixeff, fmt(%12.0fc %12.0fc %12.0fc) 							///
	label("Control mean" "\hline Individuals" "Observations" "Individual FE")) mlabels(,none)				///
	collabels(, none) eqlabels(, none) 											///
	keep(c.accountAccess#c.post) 												///
	varlabels(c.accountAccess#c.post "Account $\times$ post") 								///
	starlevels(* 0.1 ** 0.05 *** 0.01) 											///
	prehead(\begin{tabular}{l*{@M}{c}} \hline \hline \ 									///
	& Economic shock \\ ) 													///
	posthead(\hline) 													///
	prefoot( ) postfoot(\hline \hline \end{tabular})
	
 
*-------------------------------------------------------------------------------
* 2.4 Triple Difference - Consumption Cutbacks ---------------------------------
*-------------------------------------------------------------------------------
*2.4.1 Calculates table content
*-------------------------------------------------------------------------------

	eststo clear
	local y numberConsumptionCutbacks
	
	reg d.`y'
	egen t1 = max(e(sample)), by(idBaseSurvey)	
	
	reghdfe `y' c.economicShock#c.accountAccess#c.post 			/// Includes interaction of treatment x post with economic shock instead of sample restriction 
	c.accountAccess#c.post c.economicShock#c.post post if t1 == 1,		///
	abs(i.idBaseSurvey ) vce(cluster groupId)    
	
//control mean
	sum economicShock if accountAccess == 0 & post == 1 
	local control_mean = r(mean)
	if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}
		
	estadd local ind_fixeff = "Yes"	
	distinct idBaseSurvey  if e(sample)
	estadd scalar num_ind = r(ndistinct)	
	eststo
	
*-------------------------------------------------------------------------------
* 2.4.2 Writes table 
*-------------------------------------------------------------------------------
	
	esttab est* using tab_conscut_tripdiff.tex, replace 									///
	cells(b(star fmt(3)) se(par fmt(3))) label style(tex) nonumber								///
	stats( control_mean num_ind N ind_fixeff, fmt(%12.0fc %12.0fc %12.0fc) 							///
	label("Control mean" "\hline Individuals" "Observations" "Individual FE")) mlabels(,none)				///
	collabels(, none) eqlabels(, none) 											///
	keep(c.economicShock#c.accountAccess#c.post c.accountAccess#c.post c.economicShock#c.post post)				///
	varlabels(c.economicShock#c.accountAccess#c.post "Account $\times$ economic shock $\times$ post" c.accountAccess#c.post ///
	"Account $\times$ post" c.economicShock#c.post "Economic shock $\times$ post" post "Post")				///
	starlevels(* 0.1 ** 0.05 *** 0.01) ///	
	prehead(\begin{tabular}{l*{@M}{c}} \hline \hline \ ///
	Number of consumption cutbacks \\ ) ///
	posthead(\hline) ///
	prefoot( ) postfoot(\hline \hline \end{tabular})		
	
